package action;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Artical;
import model.KeyAndDetail;

import org.apache.http.NameValuePair;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import ui.main;
import util.HttpUtil;
import util.IOUtil;
import util.StringUtil;


public  class Web {
	private String loginUrl = "http://www.biquge.co/login.php?do=submit&action=login&usecookie=1&jumpurl=";

	public boolean login(String nickName, String pwd) throws Exception{
		//client = new DefaultHttpClient();
		HttpUtil.client = new DefaultHttpClient();
		Map loginMap = new HashMap();
		
		loginMap.put("password", pwd);
		loginMap.put("username", nickName);
		NameValuePair[] data = HttpUtil.fillParam(loginMap);
		//InputStream is =  doPost(client, loginUrl, data, null);
		String res = HttpUtil.doPostString(loginUrl ,data,null);
		return res.indexOf("成功")!=-1;
	}
	
	public KeyAndDetail getTitleAndTitle(String url) throws MalformedURLException, IOException{
		StringBuffer sb = new StringBuffer();
		Document doc = Jsoup.parse(new URL(url), 5000);
		String title = doc.getElementsByTag("title").get(0).text();
		title = title.substring(0,title.lastIndexOf("_"));
		Elements  es = doc.select("#list  a");
		if(es.isEmpty()) return null;
		Map<String,String> map = new HashMap<String, String>();
		List<String> titleList = new ArrayList();
		for(Element e : es){
			int inds []= {1,2};
			String txt = e.toString();
		//	System.out.println(txt);
			
			List<String> s = StringUtil.getStringsByRegex("<a href=\"([^\"]+)\">([^<]+)</a>", inds, txt);
			Artical a = new Artical();
			titleList.add(s.get(1));
			map.put(s.get(1), s.get(0));
		}
		KeyAndDetail kd = new KeyAndDetail();
		kd.setSaveTitle(title);
		kd.setTitleObj(titleList);
		kd.setDetail(map);
		return kd;
	}
	
	public String getContent(String title2,String url) throws MalformedURLException, IOException{
		String content = "";
		Document doc = Jsoup.parse(new URL(url), 5000);
		Elements  es = doc.select("#content");
		content = es.get(0).toString();
		content = content.substring(content.indexOf("-->")+3,content.length()).replaceAll("<br[^>]+>", "\r\n").replaceAll("<[\\s\\S]{0,40}>", "").replace("&nbsp;", " ");
		return content;
	}
	
	private static Web web = new Web();
	public static Web getInstance(){
		return web;
	}
	
	public static void main(String[] args) throws MalformedURLException, IOException {
		String zuie = "http://www.biquge.co/book/0/380/index.html";
		Web web =  Web.getInstance();
		KeyAndDetail kd =  web.getTitleAndTitle(zuie);
		String bookRootUrl = zuie.substring(0, zuie.lastIndexOf("/"));
		//System.out.println(articalMap.values().size());
		String path = "D:/zuie.txt";
		File writefile = new File(path);
		if(writefile.exists())writefile.delete();
		int i=0;
		List<String> ttList = kd.getTitleObj();
		for (String tt : ttList) {
			if(i++==6)break;
			String l = kd.getDetail().get(tt);
			String link = bookRootUrl +"/" +l;
			String art ="";
			Object titles [] = null;
			try {
				art = Web.getInstance().getContent(tt, link);
				IOUtil.appendMethodB(path, "\r\n"+tt+art);
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
	}
}
